package cn.zhx.xa.system.service.impl;

import cn.hutool.core.bean.BeanUtil;

import cn.zhx.xa.model.entity.SysMenu;
import cn.zhx.xa.model.vo.common.ResultCodeEnum;
import cn.zhx.xa.model.vo.menu.SysMenuSearchVo;
import cn.zhx.xa.model.vo.menu.SysMenuVo;
import cn.zhx.xa.service.exception.BusinessException;
import cn.zhx.xa.system.dao.SysMenuDao;
import cn.zhx.xa.system.dao.SysRoleMenuDao;
import cn.zhx.xa.system.mapper.SysMenuMapper;
import cn.zhx.xa.system.service.SysMenuService;
import cn.zhx.xa.system.utils.MenuHelper;
import cn.zhx.xa.utils.AuthContextUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
* @author DELL
* @description 针对表【sys_menu(菜单表)】的数据库操作Service实现
* @createDate 2024-02-02 12:20:22
*/
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Resource
    private SysMenuDao sysMenuDao;

    @Resource
    private SysMenuMapper sysMenuMapper;

    @Resource
    private SysRoleMenuDao sysRoleMenuDao;


    @Override
    public List<SysMenuSearchVo> findNodes() {
        List<SysMenu> sysMenuList = sysMenuDao.list();
        if (CollectionUtils.isEmpty(sysMenuList)) return null;
        List<SysMenuSearchVo> sysMenuSearchVos = sysMenuList.stream().map(item -> {
            SysMenuSearchVo sysMenuSearchVo = new SysMenuSearchVo();
            BeanUtil.copyProperties(item, sysMenuSearchVo);
            return sysMenuSearchVo;
        }).toList();
        return MenuHelper.buildTree(sysMenuSearchVos);
    }

    @Override
    public void save(SysMenu sysMenu) {
        //新添加节点
        sysMenuDao.save(sysMenu) ;

        // 新添加一个菜单，那么此时就需要将该菜单所对应的父级菜单设置为半开
        updateSysRoleMenuIsHalf(sysMenu) ;
    }

    private void updateSysRoleMenuIsHalf(SysMenu sysMenu) {
        // 查询是否存在父节点
        SysMenu parentMenu = sysMenuDao.getById(sysMenu.getParentId());

        if(parentMenu != null) {

            // 将该id的菜单设置为半开
            sysRoleMenuDao.updateSysRoleMenuIsHalf(parentMenu.getId()) ;

            // 递归调用
            updateSysRoleMenuIsHalf(parentMenu) ;
        }
    }

    @Override
    public void updateById(SysMenu sysMenu) {
        sysMenuDao.updateById(sysMenu);
    }

    @Override
    public void removeById(Long id) {
        int count = sysMenuDao.countByParentId(id);  // 先查询是否存在子菜单，如果存在不允许进行删除
        if (count > 0) {
            throw new BusinessException(ResultCodeEnum.NODE_ERROR);
        }
        sysMenuDao.removeById(id);		// 不存在子菜单直接删除
    }

    @Override
    public List<SysMenuVo> findUserMenuList() {
        // 获取当前登录用户的id
        Long userId = AuthContextUtil.get();

        // 查询当前用户的菜单
        List<SysMenu> sysMenuList = sysMenuMapper.selectListByUserId(userId) ;

        List<SysMenuSearchVo> sysMenuSearchVos = sysMenuList.stream().map(item -> {
            SysMenuSearchVo sysMenuSearchVo = new SysMenuSearchVo();
            BeanUtil.copyProperties(item, sysMenuSearchVo);
            return sysMenuSearchVo;
        }).toList();

        //构建树形数据
        List<SysMenuSearchVo> sysMenuTreeList = MenuHelper.buildTree(sysMenuSearchVos);
        return this.buildMenus(sysMenuTreeList);
    }

    // 将List<SysMenuSearchVo>对象转换成List<SysMenuVo>对象
    private List<SysMenuVo> buildMenus(List<SysMenuSearchVo> menus) {

        List<SysMenuVo> sysMenuVoList = new LinkedList<SysMenuVo>();
        for (SysMenuSearchVo sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenuSearchVo> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }
}




